﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace MySQL数据库操作
{
    class Program
    {
        static void Main(string[] args)
        {
            string connStr = "database=game;datasource=127.0.0.1;port=3306;user=root;password=466477891;";
            MySqlConnection connection = new MySqlConnection(connStr);

            connection.Open();

            #region 查询
            //MySqlCommand cmd = new MySqlCommand("select * from user", connection);
            //MySqlDataReader reader = cmd.ExecuteReader();
            //while (reader.Read())
            //{
            //    string username = reader.GetString("username");
            //    string password = reader.GetString("password");
            //    Console.WriteLine(username + ":" + password);
            //}
            //reader.Close();
            #endregion

            #region 插入
            //string username = "Kate";string password = "456";
            ////MySqlCommand cmd = new MySqlCommand("insert into user set username ='" + username + "'" + ",password='" + password + "'", connection);
            //MySqlCommand cmd = new MySqlCommand("insert into user set username=@un, password=@pwd", connection);
            ////cmd.ExecuteNonQuery();
            //cmd.Parameters.AddWithValue("un", username);
            //cmd.Parameters.AddWithValue("pwd", password);
            //cmd.ExecuteNonQuery();
            #endregion

            #region 删除
            //MySqlCommand cmd = new MySqlCommand("delete from user where id = @id", connection);
            //cmd.Parameters.AddWithValue("id", 3);
            //cmd.ExecuteNonQuery();
            #endregion

            #region 更新
            MySqlCommand cmd = new MySqlCommand("update user set password = @pwd where id = 5", connection);
            cmd.Parameters.AddWithValue("pwd", "我是你大爷");
            cmd.ExecuteNonQuery();
            #endregion

            connection.Close();

            Console.ReadKey();
        }
    }
}
